package com.substation.ui.dialog;

import android.app.FragmentManager;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.interfaces.ResponseListener;
import com.substation.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;


/**
 * 日期选择
 */

public class CalendarDialog extends BaseCenterFragmentDialog implements View.OnClickListener, DatePicker.OnDateChangedListener {

    private String initDate;
    private ResponseListener<String> listener;
    private String title;
    private DatePicker datePicker;
    private TextView timeTv;

    static CalendarDialog dialog;

    @Override
    protected void initViews() {
        v = inflater.inflate(R.layout.dialog_datetimer, null);
        datePicker = (DatePicker) v.findViewById(R.id.detepicker);
        v.findViewById(R.id.cancle).setOnClickListener(this);
        v.findViewById(R.id.commit).setOnClickListener(this);
        timeTv = (TextView) v.findViewById(R.id.time);
        if (title != null) {
            ((TextView) v.findViewById(R.id.title)).setText(title);
        }
        initDate(datePicker);
    }

    private void initDate(DatePicker datePicker) {
        Calendar calendar = Calendar.getInstance();
        int year = 0, month = 0, day = 0;
        String[] dateStr;
        if (!initDate.equals("")) {
            dateStr = initDate.split("-");
            year = Integer.parseInt(dateStr[0]);
            month = Integer.parseInt(dateStr[1]);
            day = Integer.parseInt(dateStr[2]);
            timeTv.setText(initDate);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            initDate = sdf.format(calendar.getTime());
            timeTv.setText(initDate);
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH)+1;
            day = calendar.get(Calendar.DAY_OF_MONTH);
        }
        datePicker.init(year, month-1, day, this);
    }

    /**
     * @param initDate -格式(1988-10-1)
     * @param listener
     * @return
     */
    public CalendarDialog setInitDateAndListner(String initDate, String title, ResponseListener<String> listener) {
        this.initDate = initDate;
        this.listener = listener;
        this.title = title;
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.commit:
                listener.response(initDate);
                break;
        }
        dismiss();
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        // 获得日历实例
        Calendar calendar = Calendar.getInstance();

        calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        initDate = sdf.format(calendar.getTime());
        timeTv.setText(initDate);
    }

    public static void getInstance(FragmentManager fragmentManager, String initDate, String title, ResponseListener<String> listener) {
        if (dialog == null) {
            dialog = new CalendarDialog();
        }
        dialog.setInitDateAndListner(initDate, title, listener);
        dialog.show(fragmentManager, "");
    }

}
